package org.zdc.pivot.wtk;
import haxe.Json;
import org.zdc.pivot.core.ISerializable;
import org.zdc.pivot.error.IllegalArgumentException;

@:final
class Dimensions implements ISerializable
{
	private static inline var serialVersionUID = -3644511824857807902;
	
	public var width:Int;
	public var height:Int;
	
	public inline static var HEIGHT_KEY:String = "height";
	public inline static var WIDTH_KEY:String = "width";
	
	public function new(width:Int, height:Int) 
	{
		this.width = width;
		this.height = height;
	}
	
	/**
	 * 判断两个对象是否相等，默认根据width和height值
	 * @param	object
	 * @return
	 */
	public function equal(object:Dynamic):Bool {
		var equal = false;
		
		if (Std.is(object, Dimensions)) {
			equal = (width == object.width) && 
					(height == object.height);
		}
		
		return equal;
	}
	
	/**
	 * 生成Dimensions
	 * @param	obj
	 * @return
	 */
	public static function make(obj:Dynamic):Dimensions {
		try {
			return new Dimensions(obj.width, obj.height);
		}
		catch (e:Dynamic) {
			return null;
		}
	}
	
	public function hashCode():Int {
		return 31 * width + height;
	}
	
	public function toString():String {
		return "[" + width + "x" + height + "]";
	}
	
	/**
	 * 解码
	 * @param	value
	 * @return
	 */
	public static function decode(value:String):Dimensions {
		if (value == "") {
			throw new IllegalArgumentException();
		}
		try {
			var json = Json.parse(value);
			return make(json);
		}
		catch (e:Dynamic) {
			return null;
		}
		
	}
}